layui.config({
    base: "static/layui/module/"
}).extend({
    dynamicCondition: 'dynamicCondition/dynamicCondition',
}).use(['form', 'layer', 'jquery', 'table', 'laydate', 'upload', 'dynamicCondition'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        dynamicCondition = layui.dynamicCondition,
        table = layui.table;

    var dataFields = object.dataFields;

    let dc = dynamicCondition.create({
        fields: dataFields
        ,tableId: "table"
        ,conditionTextId: "#findMsg"
        ,type: "complex"
        ,requestDataType: "json"
        ,counts: 10
    });

    table.on('toolbar(table)', function(obj){
        var tableCheckData = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'find':
                dc.open();
                break;
            case 'add':
                layer.open({
                    type: 2
                    ,title: '新增'
                    ,area: ['50%', '100%']
                    ,content: '/manage/common/addHtml/sys_config'
                    ,btn: ['保存', '取消']
                    ,offset: 'r'
                    ,yes: function (index, layero) {
                        var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                        var data = iframeWindow.getFormDataAndValidate();
                        if (!data[0]) {
                            return;
                        }
                        var master = data[1];

                        let dataFlag = []
                        let flag = false;
                        //数据校验
                        $.each(master,function (name,val) {
                            if (!dataFlag.includes(name)) {
                                if (!val && val !== false){
                                    flag = true;
                                }
                            }
                        })
                        if (flag){
                            layer.msg("必填项未填写！", {
                                icon: 2,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            });
                            return;
                        }
                        var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                        $.post("/manage/common/add", {url: object.url, obj: JSON.stringify(master)}, function (d) {
                            if (d.code == '200') {
                                top.layer.alert("新增成功", {icon: 1});
                                table.reload('table');
                            } else {
                                top.layer.msg("新增失败," + d.msg, {icon: 2});
                            }
                            top.layer.close(shadeIndex);
                            layer.close(index);
                        }, "json")
                    }
                });
                break;
            case 'upd':
                var data = tableCheckData.data;
                if (data.length == 0) {
                    layer.msg("请选择要修改的数据");
                    return;
                }
                if (data.length != 1) {
                    layer.msg("请选择单条数据");
                    return;
                }
                var index = layer.open({
                    type: 2
                    ,title: '修改'
                    ,area: ['50%', '100%']
                    ,content: '/manage/common/editHtml/sys_config/' + data[0].id
                    ,btn: ['保存', '取消']
                    ,offset: 'r'
                    ,yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                        var d = iframeWindow.getFormDataAndValidate();
                        if (!d[0]) {
                            return;
                        }
                        var master = d[1];
                        master.id = data[0].id;

                        var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                        $.post("/manage/common/update", {url: object.url, obj: JSON.stringify(master)}, function (d) {
                            if (d.code == '200') {
                                top.layer.msg("修改成功", {icon: 1});
                                table.reload('table');
                            } else {
                                top.layer.msg("修改失败," + d.msg, {icon: 2});
                            }
                            top.layer.close(shadeIndex);
                            layer.close(index);
                        }, "json")
                    }
                });
                break;
            case 'delete':
                if (tableCheckData.data.length == 0) {
                    layer.msg("请选择要删除的数据");
                    return;
                }
                top.layer.confirm('是否删除?', {icon: 3, title: '提示'}, function (index) {
                    var ids = new Array();
                    $.each(tableCheckData.data, function (index, e) {
                        ids.push(e.id);
                    });
                    var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                    $.ajax({
                        url: "/manage/common/delete/sys_config",
                        type: "post",
                        data: JSON.stringify(ids),
                        contentType: "application/json",
                        success: function (d) {
                            if (d.code == '200') {
                                top.layer.alert("删除成功", {icon: 1});
                                table.reload('table');
                            } else {
                                top.layer.alert("删除失败," + d.msg, {icon: 2});
                            }
                            top.layer.close(shadeIndex);
                        },
                        error: function () {
                            top.layer.close(shadeIndex);
                            alert("删除失败！");
                        }
                    });
                    top.layer.close(index);
                });
                break;
        };
    });
})